package jgl.vajra.management.part.mine.activity;

import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jgl.vajra.management.R;
import jgl.vajra.management.bean.HomeCarModelBean;
import jgl.vajra.management.bean.HomeMechanicsBean;
import jgl.vajra.management.databinding.ActivityCarAddBinding;
import jgl.vajra.management.listener.MyToolbarViewListener;
import jgl.vajra.management.manager.AccountManager;
import jgl.vajra.management.manager.IntentManager;
import jgl.vajra.management.part.mine.contract.CarAddContract;
import jgl.vajra.management.part.mine.viewModel.CarAddViewModel;
import jgl.vajra.management.utils.RandomUtils;
import jgl.vajra.management.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.management.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.management.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.management.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.management.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.management.widget.utils.UIUtils;

@CreateViewModel(CarAddViewModel.class)
public class CarAddActivity extends BaseMVVMActivity<CarAddViewModel, ActivityCarAddBinding> implements CarAddContract.View, BaseBindingItemPresenter<HomeMechanicsBean> {

    private SingleTypeBindingAdapter adapterMechanics;
    private List<HomeMechanicsBean> homeMechanicsBean;
    private SingleTypeBindingAdapter adapter;
    private int jobType;
    private String fee;
    private String equipmentName = "";
    private String equipmentType = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_car_add;
    }

    @Override
    public void initView() {
        super.initView();

        mBinding.toolbar.setMyToolbarViewListener(new MyToolbarViewListener() {
            @Override
            public void onRightTextClick() {
                super.onRightTextClick();

                Map<String, Object> mapFee = new HashMap<>();
                mapFee.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);

                mViewModel.getCarAddFeeData(mapFee);
            }
        });

        //豪车类型
        // 不可嵌套滑动
        mBinding.recyclerViewType.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerViewType.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerViewType.setLayoutManager(new LinearLayoutManager(this));
        adapterMechanics = new SingleTypeBindingAdapter(this, null, R.layout.item_car_type);
        mBinding.recyclerViewType.setAdapter(adapterMechanics);
        adapterMechanics.setItemPresenter(this);

        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_car_add);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);
        /*adapter.addSingleFootConfig(1111, R.layout.foot_car_add, null);
        adapter.setFootPresenter(this);*/

        requestNetData();
    }

    public void requestNetData() {

        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AEACCoder.encrypt(RandomUtils.getRandom()));//随机数

        mViewModel.getCarTypeData(map);
    }

    //自定义车型
    @Override
    public void onCustom() {

    }

    //选择车的型号
    @Override
    public void onCarType(int position, HomeCarModelBean itemData) {
        equipmentType = itemData.remark;
        IntentManager.toCarInformationActivity(this, itemData, jobType, fee, equipmentName, equipmentType,false);
    }

    @Override
    public void returnCarTypeBean(List<HomeMechanicsBean> homeMechanicsBean) {
        this.homeMechanicsBean = homeMechanicsBean;
        if (homeMechanicsBean != null && homeMechanicsBean.size() != 0) {
            homeMechanicsBean.get(0).isClick = true;
        }
        adapterMechanics.refresh(homeMechanicsBean);
        setTitleText(homeMechanicsBean.get(0).remark);
        getCarModelData(homeMechanicsBean.get(0).type);

        jobType = homeMechanicsBean.get(0).jobType;
        fee = homeMechanicsBean.get(0).fee;
        equipmentName = homeMechanicsBean.get(0).remark;
    }

    @Override
    public void returnHomeCarModelBean(List<HomeCarModelBean> data) {
        adapter.refresh(data);
    }

    @Override
    public void returnCarAddFeeData(String data) {
        HomeCarModelBean itemData = new HomeCarModelBean();
        IntentManager.toCarInformationActivity(CarAddActivity.this, itemData, 0, data, "", "",true);
    }

    private void getCarModelData(String type) {
        //机械型号
        Map<String, Object> mapCarModel = new HashMap<>();
        mapCarModel.put("Authorization", AEACCoder.encrypt(RandomUtils.getRandom()));
        mapCarModel.put("type", type);
        mViewModel.getCarModelData(mapCarModel);
    }

    //类型
    @Override
    public void onItemClick(int position, HomeMechanicsBean itemData) {
        if (itemData.isClick)
            return;
        for (int i = 0; i < homeMechanicsBean.size(); i++) {
            homeMechanicsBean.get(i).isClick = false;
        }
        homeMechanicsBean.get(position).isClick = true;
        adapterMechanics.refresh();
        setTitleText(itemData.remark);
        getCarModelData(itemData.type);
        jobType = itemData.jobType;
        fee = itemData.fee;
        equipmentName = itemData.remark;
    }

    public void setTitleText(String name) {
        mBinding.tvTitle.setText("请在下面选择" + name + "的型号或吨位，若实际型号不在下表中，请选择自定义车型输入你的机械型号。");
    }
}
